﻿//$(document).ready(function(){alert('test');});
var htmlGrayBg = '<div id="grayBg" style="height:100%; width:100%; position:absolute; top:0; left:0; filter:alpha(opacity=80); background:url(Images/grayBg.png) repeat; display:none; z-index:6;"></div>';
    
var htmlLoginZone = '<div id="loginZone">\
                        <div class="loginZoneNav">\
                            <a href="javascript:cancelLogin();" title="关闭"><img src="Images/cancelLogin.gif" /></a>\
                        </div>\
                        <div class="loginZoneContent clear">\
                            <div>\
                                <label>\
                                    用户名：</label>\
                                <input type="text" id="username" name="username" />\
                            </div>\
                            <div>\
                                <label>\
                                    密码：</label>\
                                <input type="password" id="password" name="password" />\
                            </div>\
                        </div>\
                        <div class="loginZoneSubmit">\
                            <input type="button" id="login1" value="登录" /><span class="loginStatus"></span>\
                        </div>\
                    </div>';

var addLoginBar = function()
{
    $("body").prepend('<div id="login_bar" class="login_bar"></div>');
    $("#login_bar").prepend('<label id="logout" class="log_button">[注销]</label><label id="login" class="log_button" onclick="javascript:showLoginWin();">[登录]</label><label id="log_status" class="log_button"></label>');
};

var showGrayBg = function()
{
    $("#grayBg").height($(document).height());
    $("#grayBg").width($(document).width());
    $("#grayBg").show();    
}

var hideGrayBg = function()
{
    $("#grayBg").hide();
}

var addLoginWin = function()
{
    $("body").append(htmlLoginZone); 
    $("body").append(htmlGrayBg);   
};

var getDimensions = function(x, y)
{
     var wWidth = 0;
     var wHeight = 0;
     //获取窗口宽度
     if (window.innerWidth)
           wWidth = window.innerWidth;
     else if ((document.body) && (document.body.clientWidth))
           wWidth = document.body.clientWidth;
     //获取窗口高度
     if (window.innerHeight)
           wHeight = window.innerHeight;
     else if ((document.body) && (document.body.clientHeight))
           wHeight = document.body.clientHeight;
   
     //通过深入Document内部对body进行检测，获取窗口大小
     if (document.documentElement  && document.documentElement.clientHeight && document.documentElement.clientWidth)
     {
         wHeight = document.documentElement.clientHeight; //IE Firefox可见区域高度
         wWidth = document.documentElement.clientWidth; //IE Firefox 可见区域宽度
     }
     
     if((x == 0) && (y != null)) return wHeight;
     else if((y == 0) && (x != null)) return wWidth;                                 
};

var showLoginWin = function()
{
    var x=((getDimensions(1,0)-300)/2).toString() + "px";
    var y=((getDimensions(0,1)-160)/2).toString() + "px";
    var grayBgHeight = document.body.scrollHeight;
    $("#loginZone").css({top:y, left:x, display:"block"});
    //$("#grayBg").css({display:"block",height:grayBgHeight});
    showGrayBg();
    $("#username").val("");
    $("#password").val(""); 
    $(".loginStatus").text("");
    if(!document.getElementById("loginZone").onkeydown)
    {
        document.getElementById("loginZone").onkeydown = enter;  
    }
}

var cancelLogin = function()
{
    $("#loginZone").css({display:"none"});
    $("#grayBg").css({display:"none"});
}

var loginBarLoginClick = function()
{
//    $("#ctl00_ctl00_login").click(function(){showLoginWin();});
    $(".login").click(function(){showLoginWin();});
}

var loginZoneLoginClick = function()
{
    $("#login1").click(test);
    function test()
    {           
        var username = $("input[name='username']").val();
        var password = $("input[name='password']").val();
        if(username == "" || password == "")
            {
                $(".loginZoneSubmit > .loginStatus").text(" 用户名或者密码不能为空！");
            }
        else{
            var userInfo = "";
            userInfo += "username=" + $("input[name='username']").val();
            userInfo += "&password=" +$("input[name='password']").val();
            //alert(userInfo);
            $.ajax({
                       type: "GET",
                       url: "ajax/validUserInfo.aspx?n=" + Math.random(),
                       data: userInfo,
                       success: function(result){
                                                    if(result !=0)
                                                    {
                                                        window.location.reload();
                                                    }
                                                    else
                                                    {
                                                        $(".loginZoneSubmit > .loginStatus").text(" 用户名或密码错误!");
                                                    }
                                                }     
                  });
            }
    }
}

var loginBarLogoutClick = function()
{
    $(".logout").click(test3);
    
    function test3()
    {
        $.ajax({
                type: "GET",
                url: "ajax/validUserInfo.aspx?n=" + Math.random() + "&action=logout",
                success: function(msg)
                          {
                            window.location.reload();
                          }                               
              });
    } 
}

//loginZone按下回车键时触发 登录 按钮单击事件；
var enter = function()
{
    if(document.getElementById("loginZone").style.display != "none")
    {
        if(event.keyCode == 13)
        {
            document.getElementById("login1").click();
        }
    }
}